import time
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait

class Base():
    # 封装driver数据驱动的方法
    def __init__(self,driver):
        self.driver = driver

    # 封装打开网址的方法
    def open_url(self,url):
        self.driver.get(url)

    # 封装当前时间+截屏的方法
    def time(self,name):
        t = time.strftime('%Y%m%d%H%M%S',time.localtime())
        self.driver.save_screenshot(t+name)

    # 封装定位元素的方法
    def find_element(self,loc):
        # 异常处理
        try:
            WebDriverWait(self.driver,20,0.5).until(lambda driver: driver.find_element(*loc).is_displayed())
            return self.driver.find_element(*loc)
        except Exception as e:
            print(f'元素定位不到{e}')
            self.time('A3mall.png')

    # 封装点击的方法
    def click(self,loc):
        self.find_element(loc).click()

    # 封装输入的方法
    def send_keys(self,loc,text):
        self.find_element(loc).send_keys(text)

    # 封装回车事件
    def Enters(self,loc):
        self.find_element(loc).send_keys(Keys.ENTER)

